package com.nami.message.domain.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.nami.message.domain.model.gmsg.GrpMsgDomain;
import com.nami.message.domain.service.GrpMsgDomainService;
import com.nami.message.domain.repository.GrpHisMsgRepository;
import com.nami.message.domain.repository.InBoxRepository;
import com.nami.message.domain.repository.SendBoxRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class GrpMsgDomainServiceImpl implements GrpMsgDomainService {

    private final GrpHisMsgRepository grpHisMsgRepository;
    private final SendBoxRepository sendBoxRepository;
    private final InBoxRepository inBoxRepository;

    @Override
    public void processReceivedMessage(GrpMsgDomain domain) {
        TimeInterval timer = DateUtil.timer();
        // 发件箱
        sendBoxRepository.addGrpMsg(domain);
        // 收件箱
        inBoxRepository.addGrpMsg(domain);
        // 历史消息
        grpHisMsgRepository.add(domain);
        if (timer.interval() > 500) {
            log.warn("[grp-add] messageId={} user:{}ms", domain.getMessageId(), timer.interval());
        }

    }

}
